package com.wn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.pojo.Order;
import com.wn.common.pojo.Rsp;
import com.wn.common.pojo.Traveller;
import com.wn.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class OrderService {
    @Autowired
      private OrderMapper orderMapper;

    /**
     * 添加订单
     * @param order
     * @return
     */
       @Transactional
          public   Rsp insertOrder(Order order){
           List<Traveller> travellers = order.getTravellers();
           String outTradeNo = UUID.randomUUID().toString();
           order.setOrderPaynumber(outTradeNo);

           int exe =  orderMapper.insertSelective(order);
           if(exe>0&&travellers!=null){
               for(int i=0;i<travellers.size();i++){
                  exe &= orderMapper.insertOrderTraveller(order.getId(),travellers.get(i).getTravellerId(),order.getCreateUserId(),new Date());
               }
              if(exe>0)
                  return Rsp.success(exe).msg("添加成功！");
              else
                  return Rsp.fail().msg("添加失败！");
           }
           else
               return Rsp.fail().msg("添加失败！");
       }



    /**
     * 修改订单
     * @param
     * @return
     */
    @Transactional
    public Rsp updateOrderInformation(Order order){
        List<Traveller> travellers = order.getTravellers();
        int exe =  orderMapper.updateByPrimaryKeySelective(order);
        if(exe>0&&travellers!=null){
            for(int i=0;i<travellers.size();i++){
                exe &= orderMapper.updateOrderTraveller(order.getId(),travellers.get(i).getTravellerId(),order.getUpdateUserId(),new Date());
            }
            if(exe>0)
                return Rsp.success(exe).msg("修改成功！");
            else
                return Rsp.fail().msg("修改失败！");
        }
        else
            return Rsp.fail().msg("修改失败！");
    }

       public Rsp updateOrder(String orderPayNumber, Date date){
           Order order = new Order();
           order.setOrderPaynumber(orderPayNumber);
           order.setOrderPaytime(date);
           return orderMapper.updateOrderStatus(order) == 1 ?
                   new Rsp().success("更新订单支付时间成功") : new Rsp().fail().msg("更新失败");
       }

    /**
     * 逻辑删除订单
     * @param id
     * @return
     */
    @Transactional
       public Rsp deleteOrder(int id,int updateUserId,Date updateTime){

        int exe =  orderMapper.deleteByPrimaryKey(id,updateUserId,updateTime);
        exe &=orderMapper.deleteOrderToTraveller(id,updateUserId);
        if(exe>0)
            return Rsp.success(exe).msg("删除成功");
        else
            return Rsp.fail().msg("删除失败！");
       }

    /**
     * 根据条件查询订单表
     */
       public Rsp orderpageInfo(Integer page,Integer pageSize,Order order){
           PageHelper.startPage(page,pageSize);

           List<Order> orderList = orderMapper.selectByCondition(order);

           PageInfo pageInfo = new PageInfo(orderList);
           if(orderList!=null)
               return Rsp.success(pageInfo).msg("查询成功！");
           else
               return Rsp.fail().msg("查询失败！");
       }

    /**
     * 根据主键查询订单表
     */
    public Rsp  getOrderById(int id){
        Order order =  orderMapper.selectByPrimaryKey(id);
        if(order!=null)
            return Rsp.success(order).msg("查询成功！");
        else
            return Rsp.fail().msg("查询失败！");

    }
}
